//
//  ShellFriendViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/26.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellFriendViewModel.h"

@interface ShellFriendViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellFriendViewModel
/**
 Convert 我的好友
 @param  isFirst 是否是第一次
 */
- (void)getMyFriend:(BOOL)isFirst{
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
    
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kMyFriends,(long)self.pageIndex];
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:url params:nil success:^(id  _Nullable responseObject) {
        @strongify(self);
        dismiss(nil);
        if([responseObject[kResultStatus] isEqualToString:kCode]){

            NSArray *dataArray = responseObject[kResultData];

             for (int i = 0; i < dataArray.count; i++) {
                 ShellFriendModel *friendModel = [ShellFriendModel mj_objectWithKeyValues:dataArray[i]];
                 [self.dataArray addObject:friendModel];
             }
        }
     
        //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
        NSInteger totalCount = [responseObject[@"total"] integerValue];
        if(self.dataArray.count == totalCount){
            //没有更多数据
            [self.reloadSubject sendNext:@(0)];
            return;
        }
        //看是否有数据
        if(self.dataArray.count == 0){
            [self.reloadSubject sendNext:@(-1)];
        } else {
            self.pageIndex++;
            [self.reloadSubject sendNext:@(1)];
        }
        
    } failure:^(NSError * _Nullable error) {
        
        [self.reloadSubject sendNext:@(0)];

    } notwork:^(NSError * _Nullable error) {
        [self.reloadSubject sendNext:@(0)];
        
    } isLoading:NO isFailTip:YES];
}


/**
 Convert 查询我的邀请信息
 */
- (void)getInviteInfo{
    [LZNetworkingManager lz_request:@"get" url:kInviteInfo params:nil success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            self.invitationModel = [ShellInvitationModel mj_objectWithKeyValues:responseObject[kResultData]];
        }
        [self.reloadSubject sendNext:@(1)];
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}
@end
